<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="110px" label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="ID" prop="id">
                <el-input v-model.trim="model.id" placeholder="请输入产品名称" disabled clearable></el-input>
            </el-form-item>
            <el-form-item label="商品SKU" prop="seller_sku">
                <el-input v-model.trim="model.seller_sku" placeholder="请输入seller_sku" clearable></el-input>
            </el-form-item>
            <el-form-item label="平台SKU" prop="platform_sku">
                <el-input v-model.trim="model.platform_sku" placeholder="请输入平台sku" clearable></el-input>
            </el-form-item>
            <el-form-item label="中文名称" prop="seller_sku_cn">
                <el-input v-model.trim="model.seller_sku_cn" placeholder="请输入中文名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="英文名称" prop="seller_sku_en">
                <el-input v-model.trim="model.seller_sku_en" placeholder="请输入英文名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="商品链接" prop="seller_sku_url">
                <el-input type="textarea" v-model.trim="model.seller_sku_url" placeholder="多个链接请以“|”分隔" :autosize="{ minRows: 3, maxRows: 6 }" clearable></el-input>
            </el-form-item>
            <el-form-item label="图片" prop="image_info">
                <ImageUploadMultiple v-model="model.image_info" :limit="5"></ImageUploadMultiple>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { editSellerSku } from '@/api/product'
export default {
    name: 'ProductDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
        type: {
            type: String,
            default: '',
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: '',
                seller_sku: '',
                platform_sku: '',
                seller_sku_cn: '',
                seller_sku_en: '',
                seller_sku_url: '',
                image_info: [],
            },
            rules: {},
        }
    },
    computed: {
        title() {
            return `编辑商品SKU`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val) {
                    this.model = Object.assign({}, this.model, val)
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {},
    methods: {
        submit() {
            const params = {
                ...this.model,
                image_info: this.model.image_info.length > 0 ? (Array.isArray(this.model.image_info) ? this.model.image_info : this.model.image_info?.split(',') ?? []) : [],
            }
            if (this.save) {
                return
            }
            this.save = true
            editSellerSku(params)
                .then(res => {
                    this.$message.success(res.msg)
                    this.dialogVisible = false
                    this.$emit('success')
                })
                .finally(() => {
                    this.save = false
                })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
.form-class.el-form .el-form-item .el-textarea {
    width: 400px;
}
</style>
